$(document).ready(function(){
	 //show book popup
	 $('.bookid').hide();
	 $("#showBook").on("click",".detail", function(){
			var bookid = $(this).children(".bookid").text();
			$.ajax({
				url: "showBookDetail.action?bookId=" + bookid,
				success:function(data){
					var dataObject = JSON.parse(data);
					/* debugger; */
					var con = "";
					var title = "";
					$(dataObject).each (function(index, value) {
						con += "<img src='" + dataObject[0].bookImage + "'/>";
						title += "<h1 class='modal-title'>"+ dataObject[0].bookName+"</h1>" + "<br>" + "<br>" + "<br>" +
						 		 "<h3 class='modal-title'>"+ dataObject[0].bookAuthor+"</h3>" + "<br>" + "<br>" +
						 		 "<h3 class='modal-title'>"+ dataObject[0].bookPress+"</h3>" ;
						 
					});
					$(".left").empty();
					$(".right").empty();
					$(".left").append(con);
					$(".right").append(title);
				}
			});
			
			$('#bookDetailPopup').slideDown("fast");
		});

	$('#closeDetailPopup').on('click', function(){
		 $('#bookDetailPopup').slideUp("fast");
	});
	
	//return book popup
 	$(".returnBookButton").click(function(){
 		var borrowId=$(this).children(".borrowIdCurrent").val();
 		var userId=$(this).children(".userIdCurrent").val();
 		$('#returnBook').slideDown("fast");
 		$("#sureReturnBook").click(function(){
 			window.location.href="showReturnBook.action?borrowId=" + borrowId + '&userid=' + userId;
 		});
 		$("#cancelReturnBook").click(function(){
 			$('#returnBook').slideUp("fast");
 		});
 	});
 	
 	//popup-borrow
 	$(".bookBorrow").click(function(){
 		var bookId=$(this).children(".borrowBookById").val();
 		var userId=$(this).children(".borrowBookByUserId").val();
 		//$('#editBookPopup').slideDown("fast");
 		$.ajax({
 			url:"showBorrowBook.action?bookId=" + bookId + '&borrowReaderid=' + userId,
 			success:function(data){
 				var dataObject=JSON.parse(data);
 				var borrowBook=dataObject["borrowBook"];
 				var borrowNumber=dataObject["borrowNumber"];
 				if(borrowNumber < 3){
 					$('#borrowBookPopup').slideDown("fast");
 					$("#borrowBookid").val(borrowBook.bookId);
 					var con = "";
						con += "<h3 class='modal-title' style='text-align:center;'>"+ "The book you want to borrow is :" + borrowBook.bookName + "</h3>" + "<br>" + "<br>";
					
					$(".showBorrowInfor").empty();
					$(".showBorrowInfor").append(con);
 				}else{
 					$('#overBookPopup').slideDown("fast");
 				}
 				}
 		});	
 	});
 	$('#closeBorrowPopup').click(function(){
 		$('#borrowBookPopup').slideUp("fast");
 	});
 	$('#overBookClose').click(function(){
 		$('#overBookPopup').slideUp("fast");
 	});
});